app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http){
    console.log('------$scope.menuId1-----', $scope.menuId);
    console.log('------$scope.buttonArr1-----', $scope.buttonArr);
    console.log('------$scope-----', $scope);

    $scope.bindBtn = function(event) {
        var datas = $scope.getSelectedRows();
        var t = this;
        switch(event){
            case 'event-to-add':
            {
                $scope.showModalRadio("THR_OperateCompanyApply_NO_ShopStopPhaseRecord", 3, 'aaaa', null, {});
                break;
            }
            case 'event-to-chooseShop':
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    $scope.doExport('/web/business/tpShopStopPhaseRecord/toChooseShop?id=' + datas[0].id + '&phaseNum=' + datas[0].phaseNum+ '&vGsBm=' + datas[0].vGsBm);
                }
                break;
            }
            case 'event-to-finish':
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var _list = [];
                    for(var i=0;i<datas.length;i++){
                        if(datas[i].workTime==undefined || datas[i].workTime==null || datas[i].workTime==''){
                            _list.push(datas[i].id);
                        }
                    }
                    var finishMsg = '确定要完成选中的跟踪单吗？';
                    if(_list==null || _list.length == 0 || _list.length == []){
                        finishMsg = '选中的跟踪单已完成，请重新选择！';
                        layer.msg(finishMsg, {icon: 0});
                        return;
                    }
                    if(_list!=null && _list.length < datas.length){
                        finishMsg = '您选中的跟踪单有部分已完成，完成时将不会更改这些信息，是否继续？';
                    }
                    layer.confirm(finishMsg, {icon: 3}, function () {
                        layer.msg('处理中', {
                            icon: 16,shade: 0.01
                        });

                        var ids = '';
                        for(var i=0;i<_list.length;i++){
                            if(i==0){
                                ids = _list[i];
                            }else {
                                ids = ids + ',' +_list[i];
                            }
                        }

                        $http.post('/api/inter/tpShopStopPhaseRecord/finishRecord', {}, {params: {ids: ids}})
                            .success(function (result) {
                                if(result.code=='0'){
                                    layer.close();
                                    layer.msg(res.msg);
                                    if(res.code==0){
                                        setTimeout(function(){location.reload();}, 2000);
                                    }
                                }else {//失败
                                    layer.close();
                                    layer.msg("处理失败，请稍后再试！", {icon: 2});
                                }
                            });

                    }, function () {});

                }
                break;
            }
            case 'event-to-delete':
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var _list = [];
                    for(var i=0;i<datas.length;i++){
                        _list[i]=datas[i].operateCompanyId;
                    }

                    layer.confirm('确定要删除选中的跟踪单吗？', {icon: 3}, function () {
                        layer.msg('处理中', {
                            icon: 16,shade: 0.01
                        });
                        var ids = '';
                        for(var i=0;i<_list.length;i++){
                           if(i==0){
                               ids = _list[i];
                           }else {
                               ids = ids + ',' +_list[i];
                           }
                        }
                        $http.post('/api/inter/tpShopStopPhaseRecord/deleteTrue', {}, {params: {ids: ids}})
                            .success(function (result) {
                                if(result.code=='0'){
                                    layer.close();
                                    location.reload();
                                    layer.msg(res.msg);
                                }else {//失败
                                    layer.close();
                                    layer.msg(result.msg, {icon: 2});
                                }
                            })
                            .error(function(result){
                                layer.close();
                                layer.msg("删除失败，请稍后再试！", {icon: 2});
                            });

                    }, function () {});

                }
                break;
            }
            case 'LAYTABLE_EXPORT_DATA':
            {
                $scope.doExport('/api/inter/tpShopStopPhaseRecord/export');
                break;
            }

        };
    }
}

//表格双击
app.controller('dbClickCtrl', ['$scope', '$http', DbClickController]);
function DbClickController($scope, $http){
    $scope.doubleClick = function(data) {
        console.log('------data-----', data);
        $scope.showModalLink('/web/business/tpShopStopPhaseRecord/toDetail?operateCompanyId=' + data.operateCompanyId, '详情');
    }
}

/*
 * 自定义通用控制器，若无定义，页面会报错
 */
app.controller('mainSelfCtrl', ['$scope', '$http', function GridSelfController($scope, $http){

    $scope.doSelectRadioBySelf = function(data) {
        console.log('单选框特殊回调', data);
        var chooseId = data.id;
        layui.layer.msg('处理中', {
            icon: 16,
            shade: 0.01
        });
        // 新增操作
        $http.post('/api/inter/tpShopStopPhaseRecord/add', {}, {params: {chooseId: chooseId}})
            .success(function (result) {
                if(result.code=='0'){
                    layer.close();
                    //刷新列表
                    setTimeout(function(){
                        location.reload();
                    }, 1000);
                    //return;
                }else {//失败
                    layer.close();
                    layer.msg(result.msg, {icon: 2});
                }
            });

    }
}]);


